package com.wxm.common.file.core.service.supplier;

import com.wxm.common.file.core.util.FileUtils;
import com.wxm.common.file.core.exception.FileException;
import com.wxm.common.file.core.property.LocalOssProperty;
import com.wxm.common.file.core.property.UploadBaseProperty;

import java.io.*;

/**
 * <p>
 *      供应商[本地对象储存]Service
 * </p>
 *
 * @author: wxm
 * @Date: 2022/1/20
 */
public class LocalOssService implements SupplierBaseService{
    private LocalOssProperty localOssProperty;

    @Override
    public void init(UploadBaseProperty uploadBaseProperty) {
        this.localOssProperty = (LocalOssProperty) uploadBaseProperty;
    }

//    @Override
//    public void uploading(String filePath) throws Exception {
//        File file = new File(filePath);
//        this.uploading(file);
//    }
//
//    @Override
//    public void uploading(byte[] uploadBytes, String name) throws Exception {
//        String nowFilePath = this.localOssProperty.getStoreAddress() + "/" + name;
//        File nowFile = FileUtils.createNewFile(nowFilePath);
//        //写入到新文件中 覆盖写入
//        this.write(nowFile,uploadBytes);
//    }
//
//    @Override
//    public void uploading(File file) throws Exception {
//        String name = file.getName();
//
//        String nowFilePath = this.localOssProperty.getStoreAddress() + "/" + name;
//        File nowFile = FileUtils.createNewFile(nowFilePath);
//        //获取源文件的字节
//        byte[] bytes = FileUtils.getBytes(file);
//        //写入到新文件中 覆盖写入
//        this.write(nowFile,bytes);
//    }

    @Override
    public void uploading(InputStream inputStream, String fileName) throws Exception {
        String nowFilePath = this.localOssProperty.getStoreAddress() + "/" + fileName;
        File nowFile = FileUtils.createNewFile(nowFilePath);

        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        byte[] bytes = bos.toByteArray();

        //写入到新文件中 覆盖写入
        this.write(nowFile,bytes);
    }

    @Override
    public void delete(String filePath) throws Exception {
        File file = new File(filePath);
        boolean b = file.delete();
        if (!b){
            FileException.error("本地文件删除失败：[" + filePath + "]请确认路径是否正确");
        }
        //判断父级的空文件夹 如果是也进行删除
        this.deleteFolder(file.getPath());
    }

    /**
     * 删除空的父级文件夹
     * @param path 路径 E:\A\B\D 则会删除 A\B\D 文件夹
     */
    private void deleteFolder(String path){
        if (path.contains("\\")) {
            File file = new File(path.substring(0, path.lastIndexOf("\\")));
            String filePath = file.getPath();
            if (file.isDirectory()) {
                if (file.list() != null || file.length() == 0) {
                    file.delete();
                    this.deleteFolder(filePath);
                }
            }
        }
    }

    /**
     * 像文件中写入内容 覆盖写入
     * @param file 文件
     * @param bytes 文件内容
     */
    private void write(File file ,byte[] bytes){
        try (OutputStream os = new FileOutputStream(file,false)){
            os.write(bytes);
        }catch (Exception e){
            FileException.error("文件写入失败");
        }
    }
}
